package com.wk.message;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class MessageUtil {
	private static final String ERROR ="ERROR" ;
	public static String sendSessage(String ip,int port,String message){
		String returnMessage = ERROR;
		DataInputStream datain = null;
		DataOutputStream dataout = null;
		Socket socket = null;
		try {
			socket = new Socket(ip,port);
			dataout= new DataOutputStream(socket.getOutputStream());
			dataout.writeUTF(message);
			dataout.flush();
			datain = new DataInputStream(socket.getInputStream());
			returnMessage = datain.readUTF();
			
		//	System.out.println("MessageUtil "+returnMessage);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				dataout.close();
				datain.close();
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return returnMessage;
		
	}
}
